package com.block.im.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.block.common.util.LocalDateUtil;
import com.block.db.entity.ImUserBaseInfo;
import com.block.db.entity.ImUserSigInfo;
import com.block.db.mapper.ImUserSigInfoMapper;
import com.block.im.config.IMConfig;
import com.block.im.enums.StatusEnum;
import com.block.im.enums.UserTypeEnum;
import com.block.im.model.dto.IMUserInfoDTO;
import com.block.im.model.param.ImUserParam;
import com.block.im.service.IImBaseCommonService;
import com.block.im.service.IImUserBaseInfoService;
import com.block.im.service.IImUserSigInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencentyun.TLSSigAPIv2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-09-24
 */
@Service
public class ImUserSigInfoServiceImpl extends ServiceImpl<ImUserSigInfoMapper, ImUserSigInfo> implements IImUserSigInfoService {

    @Autowired
    IImUserBaseInfoService imUserBaseInfoService;
    @Autowired
    IImBaseCommonService imBaseCommonService;

    @Override
    public IMUserInfoDTO genUserSig(ImUserParam imUserParam, boolean flushFlag){

        // 查询IM  用户信息
        ImUserBaseInfo imUserBaseInfo = imUserBaseInfoService.getImUserInfo(imUserParam);
        if (ObjectUtil.isEmpty(imUserBaseInfo)){
            return null;
        }

        if (!flushFlag){
            // 不强制刷新 sig
            // // 查询没有过期的  最新的一个 信息
            String datetime = LocalDateUtil.getNowDateStr(LocalDateUtil.TOTAL_DATE_TIME);
            ImUserSigInfo imUserSigInfo = this.baseMapper.getLastUseEntity(datetime, imUserBaseInfo.getUserId());
            if (ObjectUtil.isNotEmpty(imUserSigInfo)){
                IMUserInfoDTO imUserInfoDTO = new IMUserInfoDTO();
                imUserInfoDTO.setUserId(imUserSigInfo.getUserId());
                imUserInfoDTO.setUserSig(imUserSigInfo.getUserSig());
                return imUserInfoDTO;
            }
        }

        // 生成新的 sig 信息
        // 1.将现有的全部有效的sig 全部废弃/设置过期
        UpdateWrapper<ImUserSigInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", imUserBaseInfo.getUserId());
        updateWrapper.eq("status", StatusEnum.EFFECT.getValue());
        if (flushFlag){
            // 强制刷新
            updateWrapper.set("status", StatusEnum.DROP.getValue());
        }else{
            updateWrapper.set("status", StatusEnum.TIME_OUT.getValue());
        }
        boolean update = this.update(updateWrapper);

        // 生成新的sig 信息
        ImUserSigInfo imUserSigInfo = imBaseCommonService.genUserSig(imUserParam.getUserId());
        boolean save = this.save(imUserSigInfo);
        if (!save){
            return null;
        }

        IMUserInfoDTO imUserInfoDTO = new IMUserInfoDTO();
        imUserInfoDTO.setUserSig(imUserSigInfo.getUserSig());
        imUserInfoDTO.setUserId(imUserSigInfo.getUserId());
        return imUserInfoDTO;
    }

}
